﻿using System;
using Framework;
using UnityEngine;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;

namespace Game
{
    public class GameTouch : IPoolable
    {
        public int touchId { get; private set; }
        public TouchPhase touchPhase { get; private set; }
        public Vector2 screenPosition = Vector2.zero;
        public Vector2 deltaPosition = Vector2.zero;
        
        public GameTouch()
        {
        }

        public void Dispose()
        {
        }

        public void Retain(params object[] args)
        {
            this.touchId = Convert.ToInt32(args[0]);
            this.touchPhase = (TouchPhase) args[1];
            switch (touchPhase)
            {
                case TouchPhase.Moved:
                    SetupMoved((object[])args[2]);
                    break;
                default:
                    SetupBegan((object[])args[2]);
                    break;
            }
        }

        public void Release()
        {
        }

        private void SetupBegan(object[] args)
        {
            this.screenPosition.x = Convert.ToSingle(args[0]);
            this.screenPosition.y = Convert.ToSingle(args[1]);
        }

        private void SetupMoved(object[] args)
        {
            this.screenPosition.x = Convert.ToSingle(args[0]);
            this.screenPosition.y = Convert.ToSingle(args[1]);
            this.deltaPosition.x = Convert.ToSingle(args[2]);
            this.deltaPosition.y = Convert.ToSingle(args[3]);
        }
    }
}